package com.qf.dao.impl;

import com.qf.dao.CarDao;
import com.qf.model.Car;
import com.qf.model.Goods;
import com.qf.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class CarDaoImpl implements CarDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public int insertCar(Map<String, Object> map) {
        Connection con=null;
        int a=0;
        try{
            con= JDBCUtil.getCon();
            String sql="insert into cardetail (pid,cnum,csum,uid) values(?,?,?,?)";
            a = qr.update(con, sql, map.get("pid"),1,map.get("price"), map.get("uid"));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return a;
    }

    @Override
    public List<Car> selectCarByUid(Integer id) {
        Connection con=null;
        List<Car> list=null;
        try {
            con= JDBCUtil.getCon();
            //默认只查询上架的商品
            String sql="select * from cardetail where uid=?";
            list = qr.query(con,sql,new BeanListHandler<Car>(Car.class),id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }
        return list;




    }

    @Override
    public int updateCarById(int id, int cnum, BigDecimal csum) {

        Connection con=null;
        int a=0;
        try{
            con= JDBCUtil.getCon();
            String sql="update cardetail set cnum=?,csum=? where id=?";
            a = qr.update(con, sql, cnum,csum,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return a;

    }

    @Override
    public int deleteCarById(int id) {
        Connection con=null;
        int a=0;
        try{
            con= JDBCUtil.getCon();
            String sql="delete from cardetail where id=?";
            a = qr.update(con, sql,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return a;


    }

    @Override
    public Car selectCarByUidAndPid(Integer pid, Integer uid) {
        Connection con=null;
        Car car=null;
        try {
            con= JDBCUtil.getCon();
            //默认只查询上架的商品
            String sql="select * from cardetail where uid=? and pid=?";
            car = qr.query(con,sql,new BeanHandler<Car>(Car.class),uid,pid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }
        return car;

    }

    @Override
    public int updateCarByPidAndUid(Map<String, Object> map) {

        Connection con=null;
        int a=0;
        try{
            con= JDBCUtil.getCon();
            String sql="update cardetail set cnum=?,csum=? where pid=? and uid=?";
            Integer newNum=( (Car) map.get("car")).getCnum()+1;
            BigDecimal newSum=( (Car) map.get("car")).getCsum().add((BigDecimal) map.get("price"));
            Integer pid= (Integer) map.get("pid");
            Integer uid= (Integer) map.get("uid");
            a = qr.update(con, sql,newNum,newSum,pid,uid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.colseCon(con);
        }

        return a;

    }
}
